package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.oocl.dao.CustomerDao;
import com.oocl.pojo.Customer;
import com.oocl.util.DBUtil;
import com.oocl.util.StringUtil;
@Repository("customerDao")
public class CustomerDaoImpl implements CustomerDao {
	private DataSource dataSource;
	
	@Autowired
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	@Override
	public Customer addCustomer(Customer customer) {
		String sql = "insert into customer(id,cname,sex,sal,birth)values(?,?,?,?,?)";
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		PreparedStatement pst = null;
		ResultSet rs = null;
		try {
			String id = StringUtil.getRandomUUID();
			pst = conn.prepareStatement(sql);
			pst.setString(1, id);
			pst.setString(2, customer.getCname());
			pst.setBoolean(3, customer.isSex());
			pst.setDouble(4, customer.getSal());
			pst.setDate(5, new java.sql.Date(customer.getBirth().getTime()));
			if(pst.executeUpdate()>0){
				return customer;
			}
			return null;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(conn, pst, rs);
		}
		return null;
	}

	@Override
	public String deleteCustomer(String id) {
		String sql = "delete customer where id=?";
		Connection con = null;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement pst = null;
		int m = 0;
		try {

			pst = con.prepareStatement(sql);
			pst.setString(1, id);
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return "{}";
	}

	@Override
	public Customer updateCustomer(Customer customer) {
		String sql = "update customer set cname=?,sex=?,sal=?,birth=? where id=?";
		Connection con = null;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement pst = null;
		int m = 0;
		try {
			pst = con.prepareStatement(sql);
			pst.setString(1, customer.getCname());
			pst.setBoolean(2, customer.isSex());
			pst.setDouble(3, customer.getSal());
			pst.setDate(4, new java.sql.Date(customer.getBirth().getTime()));
			pst.setString(5, customer.getId());
			m = pst.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(con, pst, null);
		}
		return customer;
	}

	@Override
	public List<Customer> findAllCustomer() {
		List<Customer> list=new ArrayList<Customer>();
		String sql = "select * from customer";
		Connection con = null;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			pst = con.prepareStatement(sql);
			
			rs = pst.executeQuery();
			while(rs.next()) {
				Customer c=new Customer();
				String id = rs.getString("id");
				String cname = rs.getString("cname");
				Boolean sex = rs.getBoolean("sex");
				Double sal = rs.getDouble("sal");
				Date birth = rs.getDate("birth");
				c.setId(id);
				c.setCname(cname);
				c.setSex(sex);
				c.setSal(sal);
				c.setBirth(birth);
				list.add(c);
				
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(con, pst, rs);
		}
		return list;
	}

	@Override
	public Customer findCustomer(String id) {
		Customer c=null;
		
		String sql = "select * from customer where id=?";
		Connection con = null;
		try {
			con = dataSource.getConnection();
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		PreparedStatement pst = null;
		ResultSet rs =null;
		try {
			
			pst = con.prepareStatement(sql);
			pst.setString(1, id);;
			rs = pst.executeQuery();
			if (rs.next()) {
				c=new Customer();
				String cname = rs.getString("cname");
				boolean sex = rs.getBoolean("sex");
				double sal = rs.getDouble("sal");
				Date birth = new Date(rs.getDate("birth").getTime());
				c.setId(id);
				c.setCname(cname);
				c.setSex(sex);
				c.setSal(sal);
				c.setBirth(birth);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally{
			DBUtil.close(null, pst, rs);
		}
		return c;
	}

}
